package com.example.intenttrial;

import java.util.Timer;
import java.util.TimerTask;

import android.os.AsyncTask;
import android.widget.TextView;

public class TasksFactory {

	public static AsyncTask getSimpleTask(TrialApplication app, TextView text) {
		return new SimpleTask(app, text);
	}

	public static AsyncTask getTaskWithTimer(TrialApplication app, TextView text) {
		return new TaskWithTimer(app, text);
	}

}

class SimpleTask extends AsyncTask {
	protected TextView text;
	protected TrialApplication app;

	public SimpleTask(TrialApplication app, TextView text) {
		this.app = app;
		this.text = text;
	}

	@Override
	protected Object doInBackground(Object... params) {
		app.incrementCounter();
		return null;
	}

	@Override
	protected void onPostExecute(Object result) {
		super.onPostExecute(result);

		app.setText("Message: " + app.getCounter());
		text.setText(app.getText());
	}

}

class TaskWithTimer extends SimpleTask {

	public TaskWithTimer(TrialApplication app, TextView text) {
		super(app, text);
	}

	@Override
	protected Object doInBackground(Object... params) {
		try {
			Thread.sleep(3000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		app.incrementCounter();
		return null;
	}
	
	@Override
	protected void onPostExecute(Object result) {
		
		app.setText("Message delayed: " + app.getCounter());
		text.setText(app.getText());
	}

}